package com.taiji.dianthus.controller;

import com.alibaba.fastjson.JSONArray;
import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.sms.SmsInfoService;
import com.taiji.dianthus.service.sys.OrgService;
import com.taiji.dianthus.service.sys.RoleUserService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.service.sys.UserService;
import com.taiji.dianthus.util.IPUtil;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_QUERY;

/**
 * @ClassName HomeController
 * @Description 登陆、文档入口
 * @Author H.M
 * @Date 2019/9/6
 */
@RequestMapping(value = "/home")
@Controller
public class HomeViewController extends BasicController {

    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private OrgService orgService;

    @Autowired
    private SmsInfoService smsInfoService;

    @Autowired
    private RoleUserService roleUserService;

    @Value("${safty_role}")
    private String saftyRole;//安全管理员
    @Value("${system_role}")
    private String systemRole;//系统管理员
    @Value("${audit_role}")
    private String auditRole;//审计管理员
    @Value("${user_role}")
    private String userRole;//普通用户

    /**
     * 跳转首页
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/index")
    public String showIndex(Model model, HttpServletRequest request, String id, String flag, String startDate, String endDate, Integer currentPage, @ModelAttribute("loginPage") String loginPage) {
        DSysUser user = currentUser();
        SmsInfo info = null;
        if (!StringUtils.isEmpty(id)) {
            info = smsInfoService.getOne(id);
        } else {
            info = new SmsInfo();
        }
        if (!StringUtils.isEmpty(loginPage) && !"".equals(loginPage)) {
            model.addAttribute("loginPage", loginPage);
        }
        if (!StringUtils.isEmpty(flag)) {//判断页面显示取消还是返回 同时返回到哪个页面
            if (("drafts").equals(flag)) {
                String url = "/sms/drafts/list?pageSize=10";
                if (!StringUtils.isEmpty(startDate)) {
                    url += "&startDate=" + startDate;
                }
                if (!StringUtils.isEmpty(endDate)) {
                    url += "&endDate=" + endDate;
                }
                if (currentPage != null) {
                    url += "&currentPage=" + currentPage;
                }
                model.addAttribute("url", url);
            } else {
                String url = "/sms/info/list?pageSize=10";
                if (!StringUtils.isEmpty(startDate)) {
                    url += "&startDate=" + startDate;
                }
                if (!StringUtils.isEmpty(endDate)) {
                    url += "&endDate=" + endDate;
                }
                if (currentPage != null) {
                    url += "&currentPage=" + currentPage;
                }
                model.addAttribute("url", url);
            }
        }
        String roleId = roleUserService.getRoleIdByUserCode(user.getId());//当前登录用户角色
        if (!StringUtils.isEmpty(roleId) && !"".equals(roleId) && auditRole.equals(roleId)) {//审计管理员
            return "redirect:/sys/log/list";
        }
        if (!StringUtils.isEmpty(roleId) && !"".equals(roleId) && (saftyRole.equals(roleId) || systemRole.equals(roleId))) {//安全管理员  //系统管理员
            return "redirect:/sys/org/treeList";
        }
        model.addAttribute("data", info);
        model.addAttribute("flagStr", flag);
        JSONArray treeObj = orgService.getOrgUserTree();
        model.addAttribute("tree", treeObj);
        model.addAttribute("currentUser", user);
        model.addAttribute("menus", listMenu());
        sysLogService.save(user, LOG_OPERATION_QUERY,
                IPUtil.getIpAddr(request), "查询首页", 1, "查询成功");
        return "/pages/sms/sendMessage";
    }

    @RequestMapping(value = "/session/invalid")
    public String sessionInvalid(Model model, HttpServletRequest request) {
        return "/pages/common/session_invalid";
    }

}
